package leetcode.oldVersion;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class lc438V2 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> result = new ArrayList<Integer>();
        int lenS = s.length();
        int lenP = p.length();
        if (lenS<lenP) {
            return new ArrayList<Integer>();
        }
        int[] arrS = new int[26];
        int[] arrP = new int[26];
        for (int i = 0; i < lenP; ++i) {
            arrS[s.charAt(i) - 'a']++;
            arrP[p.charAt(i) - 'a']++;
        }

        if (Arrays.equals(arrS, arrP)) {
            //绗竴涓獥鍙ｄ腑鐨刢har瀛楃绫诲瀷涓庢暟閲忕浉鍚岋紝閰嶅鎴愬姛锛屽皢瀹冨姞鍏�
            result.add(0);
        }

        for (int i = 0; i < lenS-lenP; i++) {//闇€瑕佽繖涔堝涓獥鍙ｆ暟閲�
            arrS[s.charAt(i)-'a']--;
            arrS[s.charAt(i+lenP)-'a']++;

            if (Arrays.equals(arrS, arrP)) {
                result.add(i+1);
            }
        }

        return result;
    }
}
